﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Designer.UI
{
    public class CommandCenter
    {
        /// <summary>
        /// 执行的命令列表
        /// </summary>
        public Stack<Command> DoList { get; set; }

        /// <summary>
        /// 撤销执行的命令列表
        /// </summary>
        public Stack<Command> UndoList { get; set; }

        public CommandCenter()
        {
            DoList = new Stack<Command>();
            UndoList = new Stack<Command>();
        }

        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="cmd"></param>
        public void Do(Command cmd)
        {
            cmd.Do();
            DoList.Push(cmd);
            UndoList.Clear();
        }

        /// <summary>
        /// 撤销命令执行
        /// </summary>
        public void Undo()
        {
            if (DoList.Count == 0)
            {
                return;
            }

            Command cmd = DoList.Pop();
            UndoList.Push(cmd);
            cmd.Undo();
        }

        /// <summary>
        /// 重新执行撤销的命令
        /// </summary>
        public void Redo()
        {
            if (UndoList.Count == 0)
            {
                return;
            }

            Command cmd = UndoList.Pop();
            DoList.Push(cmd);
            cmd.Do();
        }
    }
}
